import $ from '../common';
Page({
  data: {
    code: '',
    password: '',
    newpassword:'',
    countdown: 60,
    disabledCode: true,
    isfirst: true,
    istip: false,
    ner: ''
  },
  errtip(tit) {//错误提示
    this.setData({
      istip: true,
      ner: tit
    })
    setTimeout(() => {
      this.setData({
        istip: false,
      })
    }, 2000);
  },
  bindphoneInput(e){//phone 输入
    this.setData({
      phone:e.detail.value
    })
  },
  bindKeyInput(e) { //code输入
    this.setData({
      code: e.detail.value
    })
  },
  bindpasswordInput(e) { //密码输入
    let str = e.detail.value.replace(/\s*/g, "");
    this.setData({
      password: str
    })
  },
  bindNewPassInput(e){
    let str = e.detail.value.replace(/\s*/g, "");
    this.setData({
      newpassword: str
    })
  },
  clearcode() { //清除验证码
    this.setData({
      code: ''
    })
  },
  getcode() {//获取验证码
    let countdown = this.data.countdown
    this.setData({
      isfirst: false
    })
    let _fn = () => {
      if (countdown == 0) {
        this.resetDentify()
      } else {
        this.disabledCode = false
        this.setData({
          countdown: countdown,
          disabledCode: this.disabledCode,
        })
        if (countdown == 60) {
          this.getcodehand()
        }
        countdown--
      }
    }
    _fn()
    this.setTimeDentify = setInterval(() => {
      _fn()
    }, 1000)
  },
  getcodehand() { //获取验证码的请求
    $.get("/verifycode", {
      phone: this.data.phone,
      type: 'RESET'
    }, (data) => {
      $.tip("验证码已发送到尾号为" + (this.data.phone).replace(/\s/g, "").toString().substring(7) + "的手机上，请注意查收!")
    }, (res) => {
      $.tip(res.message)
      this.resetDentify()
    })
  },
  resetDentify() { //当60s结束后
    clearInterval(this.setTimeDentify)
    this.disabledCode = true
    this.countdown = 60
    this.setData({
      countdown: this.countdown,
      disabledCode: this.disabledCode
    })
  },
  submit() { //提交
    const {code,password,newpassword,phone,type} = this.data
    if (!code) {
      this.errtip('请先输入验证码')
      return
    }
    if (!password) {
      this.errtip('请先输入新密码')
      return
    }
    if(type=='register'){
      if(!newpassword){
        this.errtip('请再次输入新密码')
        return
      }
      if(newpassword!=password){
        this.errtip('输入的两次密码不一致')
        return
      }
    }

    // /user/signup//注册账号
    // /user/add/student//添加子女
    ///user/add/registrationid 获取用户设备id
    if(type=='Modify'){
      $.post('/user/passwd/reset', {
        verify: code,
        passwd: password
      }, res => {
        $.get("/user/logout", {}, (e) => {
          wx.showToast({
            title: '修改密码成功,正在前往登录',
            icon: 'none',
            duration: 2000,
            success: res => {
              setTimeout(() => {
                wx.removeStorageSync('token')
                wx.removeStorageSync('phone')
                wx.removeStorageSync('addresscode')
                wx.removeStorageSync('address')
                wx.reLaunch({
                  url: '../login/index?amend=true'
                })
              }, 2000);
            }
          })
        })
      }, err => {
        this.errtip(err.message)
      })
    }else if(type=='register'){
      $.post('/user/signup',{
        phone,
        verify:code,
        passwd:password,
        passwd_verify:newpassword
      },res=>{
  
      })
    }

  },
  onLoad(e) {
    this.setData({
      phone: e.phone||'',
      type:e.type
    })
    wx.setNavigationBarTitle({
      title: e.type=='register'?'注册账号':'修改密码',
    })
  },
  onShow() {},
})